package com.it.web;

import com.it.entity.survey.AiSurveyReport;
import com.it.service.AiSurveyReportService;
import com.it.vo.ReportVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import utils.R;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yzy
 * @since 2025年09月20日
 */
@RestController
@RequestMapping("/aiSurveyReport")
public class AiSurveyReportController {

    @Autowired
    private AiSurveyReportService aiSurveyReportService;

    //返回对应报案号的已查勘信息
    @GetMapping("/{reportNo}")
    public R getAiSurveyReport(@PathVariable("reportNo") String reportNo){
        return aiSurveyReportService.getAiSurveyReport(reportNo);
    }

    @PutMapping("/save")
    public R save(@RequestBody AiSurveyReport aiSurveyReport){
        try {

            aiSurveyReportService.updateByReportNo(aiSurveyReport);
            return R.Success("更新成功");
        } catch (Exception e) {
            return R.Failed("更新失败：" + e.getMessage());
        }
    }


    @PostMapping("/select")
    public R select(@RequestBody ReportVo reportVo,
                    @RequestParam(value = "pageNum",required = false,defaultValue = "1") Integer pageNum,
                    @RequestParam(value = "pageSize",required = false,defaultValue = "2") Integer pageSize) {
        try {

            return R.Success("查询成功",aiSurveyReportService.select(reportVo,pageNum,pageSize));
        } catch (IllegalArgumentException e) {
            // 处理参数异常
            return R.Failed(e.getMessage());
        } catch (Exception e) {
            // 处理其他异常
            return R.Failed("查询失败：" + e.getMessage());
        }
    }


    @GetMapping("/selectDispatchList")
    public R selectDispatchList(@RequestParam(value = "pageNum",required = false,defaultValue = "1") Integer pageNum,
                                @RequestParam(value = "pageSize",required = false,defaultValue = "2") Integer pageSize,
                                @RequestParam (value = "reportId",required = false) String reportId){
        return R.Success("查询成功",aiSurveyReportService.selectDispatchList(pageNum,pageSize,reportId));
    }

    @PutMapping("/confirmDispatch/{dispatchId}")
    public R confirmDispatch(@PathVariable int dispatchId,
                             @RequestParam(value = "surveyInWorkNo",required = false) String surveyInWorkNo,
                             @RequestParam(value = "reportNo",required = false) String reportNo){
        aiSurveyReportService.updateStatus(dispatchId);
        aiSurveyReportService.addSurvey(surveyInWorkNo,reportNo);
        return R.Success("确认成功");
    }

    @PutMapping("/rejectedDispatch/{dispatchId}")
    public R rejectedDispatch(@PathVariable int dispatchId,@RequestParam(value = "cancelReason",required = false) String cancelReason){
        aiSurveyReportService.rejectedDispatch(dispatchId,cancelReason);
        return R.Success("取消成功");
    }

    @GetMapping("/getDispatch")
    public R getDispatch(@RequestParam(value = "pageNum",required = false,defaultValue = "1") Integer pageNum,
                         @RequestParam(value = "pageSize",required = false,defaultValue = "2") Integer pageSize,
                         @RequestParam (value = "reportId",required = false) String reportId){
        return aiSurveyReportService.getDispatch(pageNum,pageSize,reportId);
    }

    @PostMapping("/singleImage")
    public R singleImage(@RequestBody MultipartFile file){
        return aiSurveyReportService.singleImage(file);
    }

    @DeleteMapping("/deleteImage")
    public R deleteImage(String url){
        return aiSurveyReportService.deleteImage(url);
    }

//    //状态变为已查勘
//    @PutMapping("/updateStatus")
//    public R updateStatus(@RequestParam (value = "reportId",required = false) String reportId){
//        aiSurveyReportService.updateStatusB(reportId);
//        return R.Success("更新查勘状态成功");
//    }
    //查询已查勘列表
    @GetMapping("/selectDispatch")
    public R selectDispatch(@RequestParam(value = "pageNum",required = false,defaultValue = "1") Integer pageNum,
                            @RequestParam(value = "pageSize",required = false,defaultValue = "5") Integer pageSize,
                            @RequestParam (value = "reportNo",required = false) String reportNo,
                            @RequestParam (value = "surveyInWorkNo",required = false) String surveyInWorkNo){
        return aiSurveyReportService.selectDispatch(pageNum,pageSize,reportNo,surveyInWorkNo);
    }

}
